package com.company;

import java.util.*;

/**
 * @author jrue
 * @version 1.0
 * @date 2020/3/22 10:52
 */
public class Main32 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] split = s.split("\\|");

        HashMap<Integer,Integer> map = new HashMap<>();

        String[] strings = split[0].split(";");
        for (int i = 0; i < strings.length; i++) {
            String[] strings1 = strings[i].split(",");
            int key = Integer.valueOf(strings1[0]);
            int value = Integer.valueOf(strings1[1]);
            map.put(key,value);
        }
        String[] split1 = split[1].split(",");
        List<Integer> start = new ArrayList(split1.length);
        for (int i = 0; i < split1.length; i++) {
            start.add(Integer.valueOf(split1[i]));
        }

        int end = Integer.valueOf(split[2]);
        Set<Integer> res = new TreeSet<>();
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (start.contains(entry.getKey())) {
                ArrayList<Integer> tempList = new ArrayList<>();
                tempList.add(entry.getKey());
                int node = entry.getValue();
                boolean flag = true;
                while (node != end) {
                    if (map.containsKey(node) && !tempList.contains(node)) {
                        tempList.add(node);
                        node = map.get(node);
                    } else {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    res.addAll(tempList);
                }
            }
        }

        Object[] array = res.toArray();
        for (int i = 0; i < array.length; i++) {
            if (i == array.length - 1) System.out.print(array[i]);
            else System.out.print(array[i] + ",");
        }

    }
}
